package com.myapplication.ui.infofragment;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.myapplication.R;
import com.myapplication.bean.ZiXunBean;

import java.util.List;

/**
 * Created by My on 2016/10/8.
 */
public class ZiXunfragmentAdapter extends BaseAdapter{
    private Context context;
    private List<ZiXunBean.DataBean.ListBean> ziXunBeanList;
    private static final int item_type1=0;
    private static final int item_type2=1;
    private static final int item_count=2;

    public void setZiXunBeanList(List<ZiXunBean.DataBean.ListBean> ziXunBeanList) {
        this.ziXunBeanList = ziXunBeanList;
    }

    public ZiXunfragmentAdapter(Context context, List<ZiXunBean.DataBean.ListBean> ziXunBeanList){
        this.context=context;
        this.ziXunBeanList=ziXunBeanList;
    }

    @Override
    public int getCount() {
        return ziXunBeanList!=null?ziXunBeanList.size():0;
    }

    @Override
    public Object getItem(int i) {
        return ziXunBeanList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return  i;
    }



    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder1 holder1=new ViewHolder1();
        ViewHolder2 holder2=new ViewHolder2();
        int item_type=getItemViewType(i);
        //listview的复用
        if(view==null){
            switch (item_type){
                case item_type1:
                    view= LayoutInflater.from(context).inflate(R.layout.item_lv_zixuntype1,null);
                    holder1.iv_imageUrl1= (ImageView) view.findViewById(R.id.iv_zx_image_type1);
                    holder1.tv_title1= (TextView) view.findViewById(R.id.tv_zx_title_type1);
                    holder1.tv_summary1= (TextView) view.findViewById(R.id.tv_zx_summary_type1);
                    view.setTag(holder1);
                    break;
                case item_type2:
                    view=LayoutInflater.from(context).inflate(R.layout.item_lv_zixuntype2,null);
                    holder2.iv_imageUrl2= (ImageView) view.findViewById(R.id.iv_zx_image_type2);
                    holder2.tv_title2= (TextView) view.findViewById(R.id.tv_zx_title_type2);
                    holder2.tv_summary2= (TextView) view.findViewById(R.id.tv_zx_summary_type2);
                    view.setTag(holder2);
                    break;
            }
        }else{
            switch (item_type){
                case item_type1:
                    holder1= (ViewHolder1) view.getTag();
                    break;
                case item_type2:
                    holder2= (ViewHolder2) view.getTag();
                    break;
            }
        }
        //给布局设置内容
        switch (item_type){
            case item_type1:
                holder1.tv_summary1.setText(ziXunBeanList.get(i).getHuiZixun().getSummary());
                holder1.tv_title1.setText(ziXunBeanList.get(i).getHuiZixun().getTitle());
                Glide.with(context)
                        .load(ziXunBeanList.get(i).getHuiZixun().getImageUrl())
                        .into(holder1.iv_imageUrl1);
                break;
            case item_type2:
                holder2.tv_title2.setText(ziXunBeanList.get(i).getHuiZixun().getTitle());
                holder2.tv_summary2.setText(ziXunBeanList.get(i).getHuiZixun().getSummary());
                Glide.with(context)
                        .load(ziXunBeanList.get(i).getHuiZixun().getImageUrl())
                        .into(holder2.iv_imageUrl2);
                break;
        }
        return view;
    }

    @Override
    public int getViewTypeCount() {
        return item_count;
    }

    @Override
    public int getItemViewType(int position) {
        if (ziXunBeanList.get(position).getHuiZixun() != null) {
            Log.i("TAG", "getItemViewType: "+ziXunBeanList.get(position).getHuiZixun().toString());
            return ziXunBeanList.get(position).getHuiZixun().isIsSmall()==false?item_type1:item_type2;
        }
        return 0;
    }

    static class ViewHolder1{
        ImageView iv_imageUrl1;
        TextView tv_title1,tv_summary1;
    }
    static class ViewHolder2{
        ImageView iv_imageUrl2;
        TextView tv_title2,tv_summary2;
    }
}
